import { ProductList } from "list-model.js";
var productlist = new ProductList();


var selectIndex;
var attrIndex;
var num = 1000000;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnChange: true,
    productCounts: 1,
    loadingHidden: false,
    searchLoadingComplete: false,
    searchLoading: false,
    page: 1,
    flag:0,
    params:{
      'proming_status': 1,
    }
  },
  onLoad: function (options) {
    this._loadData();
  },

  /*加载数据*/
  _loadData: function () {
    var that = this;
    productlist.getPromProductData(1,num,this.data.params, (res) => {
      var data = res.data.items;
      var endTimeList = [];
      for (var i = 0; i < data.length; i++) {
        endTimeList.push(data[i].end_time)
      }
      this.setData({
        actEndTimeList: endTimeList,
        promData: res.data.items,
        pageCount: res.data._meta.pageCount,
        loadingHidden: true,
      })
      this.countDown();
      if (res.data._meta.pageCount == 1) {
        that.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
    });
  },

  // //秒杀倒计时
  countDown: function () {
    var nowtime = Date.parse(new Date()) / 1000;
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = {};
    for (var i = 0; i < endTimeList.length; i++) {
      var endtime = endTimeList[i];
      var obj = null;
      var second = endtime - nowtime;
      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;

      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;

      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;

      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;
      countDownArr = {
        day: dayStr,
        hou: hrStr,
        min: minStr,
        sec: secStr
      }

      this.data.promData[i]['countDownArr'] = countDownArr;

      
    }

      this.setData({ promData: this.data.promData})

   this.data.timer  = setTimeout(this.countDown, 1000);
  },

  // 改变商品列表列数
  changeBtn: function () {
    this.setData({
      btnChange: !this.data.btnChange
    })
  },


  // //滚动加载
  // lower: function (e) {
  //   var page = parseInt(this.data.page) + 1;
  //   if (page <= this.data.pageCount) {
  //     this.setData({
  //       searchLoading: true,
  //     })
  //     productlist.getPromProductData(page, 4, this.data.params, (res) => {
  //       this.scrollLoadData(res, page);
  //     })
  //   }
  // },

  // scrollLoadData: function (res, page) {
  //   var promData = this.data.promData;
  //   var data = res.data.items;
  //   var endTimeList = [];
  //   for (var i = 0; i < data.length; i++) {
  //     endTimeList.push(data[i].end_time)
  //   }
  //   this.setData({
  //     page: page,
  //     flag:1,
  //   })

  //   if (page == this.data.pageCount) {
  //     this.setData({
  //       searchLoading: false,
  //       searchLoadingComplete: true,
  //     })
  //   }
  // },

  /*跳转到商品详情*/
  onPromToDetailItemTap: function (event) {
    var id = productlist.getDataSet(event, 'id');
    var skuid = productlist.getDataSet(event, 'skuid');
    wx.navigateTo({
      url: '../product/product?id=' + id + '&skuid=' + skuid
    })
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '限时秒杀'
    }
  },
  //页面退出时清除定时器
  onUnload: function () {
    clearTimeout(this.data.timer)
  },

})